package com.orange.controller;


import com.github.pagehelper.PageInfo;
import com.orange.commons.Result;
import com.orange.domain.Notice;
import com.orange.dto.NoticeDto;
import com.orange.service.NoticeService;
import com.orange.utils.WebMvcUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author Emp
 * @projectName: crm
 * @package: com.powernode.controller
 * @className: NoticeController
 * @description: TODO
 * @date: 2021/12/2 19:35
 * @version: 1.0
 */
@RestController
@RequestMapping("notice")
public class NoticeController {

    @Resource
    private NoticeService noticeService;

    @RequestMapping("list.do")
    public Result list(NoticeDto noticeDto){
        PageInfo<Notice> pageInfo = noticeService.queryByPage(noticeDto);
        return new Result(0,"查询成功",pageInfo.getTotal(),pageInfo.getList());
    }

    @RequestMapping("sysList.do")
    public Result sysList(){
        List<Notice> list = noticeService.queryAll();
        return new Result(0,"查询成功",0L,list);
    }

    @RequestMapping("addNotice.do")
    public Result addNotice(Notice notice){
        try {
            notice.setId(null);
            notice.setCreateTime(new Date());
            notice.setAuthor(WebMvcUtil.getCurrentUserName());
            noticeService.insertSelective(notice);
            return Result.USER_ADD_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return Result.USER_ADD_ERROR;
        }
    }

    @RequestMapping("delNotice.do")
    public Result delNotice(Integer id){
        try {
            noticeService.deleteByPrimaryKey(id);
            return Result.USER_DEL_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return Result.USER_DEL_ERROR;
        }
    }

    @RequestMapping("updateNotice.do")
    public Result updateNotice(Notice notice){
        try {
            noticeService.updateByPrimaryKeySelective(notice);
            return Result.USER_UPDATE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return Result.USER_UPDATE_ERROR;
        }
    }


}
